#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>          /* See NOTES */
#include<sys/socket.h>
#include<unistd.h>
#include<arpa/inet.h>
#include<string.h>

#define N 128

#define ERRLOG(errmsg) do{\
                                                                perror(errmsg);\
                                                                printf("file : %s, func: %s, line : %d", __FILE__, __func__, __LINE__);\
                                                                exit(-1);\
                                                         }while(0);

int main(int argc, char const *argv[]){
    if(argc != 3){
        fprintf(stderr, "Usage %s <ip> <port>\n", argv[0]);
        exit(-1);
    }
    int sockfd = -1;
    struct sockaddr_in servaddr;
    socklen_t addrlen = sizeof(servaddr);
    //第一步：创建套接字
    if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){
        ERRLOG("socket error");
    }

    //第二步：填充ip4网络信息结构体
    memset(&servaddr, 0, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = inet_addr(argv[1]);
    servaddr.sin_port = htons(atoi(argv[2]));


    //第三步：发送客户端连接请求
    if((connect(sockfd, (struct sockaddr *)&servaddr, addrlen)) == -1){
        ERRLOG("accepet error");
    }

    //进行通信
    while(1){
        if(send(sockfd, "hello world!", sizeof("hello world!"), 0) == -1){
            ERRLOG("send error");
        }
        sleep(3);
    }
    return 0;
}
